package com.hs.panghu.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.hs.panghu.dao.CommunityDao;
import com.hs.panghu.dao.GoodsDao;
import com.hs.panghu.dao.imp.CommunityDaoImpl;
import com.hs.panghu.dao.imp.GoodsDaoImpl;
import com.hs.panghu.entity.Goods;
import com.hs.panghu.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/goods")
public class GoodsServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        //            连接数据库
        GoodsDao goodsDao = new GoodsDaoImpl();
//        分页查询
        if("querySearchShopGoodsList".equals(action)){
            Map<String,String> map = new HashMap<>();
            String startTime = req.getParameter("startTime");
            String endTime = req.getParameter("endTime");
            String shopName = req.getParameter("shopName");
            String goodsStatus = req.getParameter("goodsStatus");
            String goodsName = req.getParameter("goodsName");
            String pageSize = req.getParameter("pageSize");
            String pageNum = req.getParameter("pageNum");

            map.put("startTime",startTime);
            map.put("endTime",endTime);
            map.put("shopName",shopName);
            map.put("goodsStatus",goodsStatus);
            map.put("goodsName",goodsName);
            map.put("pageSize",pageSize);
            map.put("pageNum",pageNum);


            List<Goods> list = goodsDao.queryGoods(map);

//            动态获取社区的数据总条数
            int total = goodsDao.queryCounts(map);
//            创建一个返回值的Map集合，用来封装list数据和total总条数
            Map resultMap = new HashMap();
            resultMap.put("list",list);
            resultMap.put("total",total);
//            把结果集合，封装到result对象中
            Result success = Result.success(resultMap);
            resp.getWriter().write(JSON.toJSONString(success));
        }
//        上下架
        if("upOrDown".equals(action)){
            Integer goodsId = Integer.valueOf(req.getParameter("goodsId"));
            Integer goodsStatus = Integer.valueOf(req.getParameter("goodsStatus"));
            Goods goods = new Goods(
                    goodsId,
                    goodsStatus
            );

            int i = goodsDao.editGoods(goods);
            if(i>0){
                Result success = Result.success();
                resp.getWriter().write(JSON.toJSONString(success));
            }else{
                Result error = Result.error();
                resp.getWriter().write(JSON.toJSONString(error));
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
